package org.kjk.mail;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GMailContentJsonController extends AbstractController {
	private GMail gmail;
	private String id = null;
	private String pass = null;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		id = (String) request.getSession().getAttribute("id");
		pass = (String) request.getSession().getAttribute("id");
		if (id == null) {
			return new ModelAndView("/mail/gmailLogin.jsp");
		}
		
		gmail.setAccountDetails(id, pass);
		
		String index = request.getParameter("index");
		if (index == null || index.length() == 0) {
			index = "0";
		}
		JSONObject json = new JSONObject();
		json.putOnce("content", gmail.getContent(Integer.parseInt(index)));
		response.getWriter().println(json);
		
//		response.getWriter().println(gmail.getContent(Integer.parseInt(index)));
		return null;
	}

	public void setGmail(GMail gmail) {
		this.gmail = gmail;
	}

}
